/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.SejourController;
import Model.Agenda;
import Model.Sejour;
import Model.TestData;
import java.beans.PropertyChangeEvent;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Hugo
 */
public class ChangerChambre extends AbstractViewPanel {

    /**
     * Creates new form ModifierSejour
     */
    private SejourController controller;
    private Sejour sejour;
    
    public ChangerChambre(SejourController controller, Sejour sejour) {
        initComponents();
        this.controller = controller;
        this.sejour = sejour;
        populerTableSejour(Agenda.getInstance().lireChambreSejour(sejour.getNum()));
        populerTableChambreLibre();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblChambreSejour = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblChambreDispo = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        btnRemplacer = new javax.swing.JButton();

        tblChambreSejour.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblChambreSejour);

        jLabel2.setText("Chambres du séjour (sélectionner la chambre à changer)");

        tblChambreDispo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tblChambreDispo);

        jLabel3.setText("Chambres disponibles (Sélectionner la chambre pour remplacer)");

        btnRemplacer.setText("Remplacer");
        btnRemplacer.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnRemplacerMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(127, Short.MAX_VALUE)
                .addComponent(btnRemplacer)
                .addGap(156, 156, 156))
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(11, 11, 11)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(52, 52, 52)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(btnRemplacer)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnRemplacerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnRemplacerMouseClicked
        if(tblChambreDispo.getSelectedRows().length != 0){
            if(tblChambreSejour.getSelectedRows().length != 0){
                
                
                controller.changeChambre(Integer.parseInt(tblChambreSejour.getValueAt(tblChambreSejour.getSelectedRow(),0).toString()),
                        Integer.parseInt(tblChambreDispo.getValueAt(tblChambreSejour.getSelectedRow(),0).toString()));
                
                JOptionPane.showMessageDialog(null, "La chambre a été changé");
                populerTableSejour(Agenda.getInstance().lireChambreSejour(sejour.getNum()));
                populerTableChambreLibre();
                  //this.getTopLevelAncestor().setVisible(false);
            } else{
                JOptionPane.showMessageDialog(this, "Vous dever sélectionner la chambre à changer.");
            }  
        }else{
            JOptionPane.showMessageDialog(this, "Vous dever sélectionner la chambre que vous voulez.");
        }
    }//GEN-LAST:event_btnRemplacerMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRemplacer;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblChambreDispo;
    private javax.swing.JTable tblChambreSejour;
    // End of variables declaration//GEN-END:variables

    private void populerTableSejour(String[][] listeChambre){
        String[] celNames;
        
        celNames = new String[2];
        celNames[0] = "Numéro de la chambre";
        celNames[1] = "Catégorie";

        JTable jtbl = new JTable(listeChambre, celNames);
        this.tblChambreSejour.setModel(new DefaultTableModel());
        this.tblChambreSejour.setModel(jtbl.getModel());        
    }
    
    private void populerTableChambreLibre(){
        String[][] listeChambre = Agenda.getInstance().AfficherChambreLibreParCategorie(sejour.getDateDebut(), sejour.getDateFin(), sejour.getListeChambre().get(0).getCategorie());
        String[] celNames;
        
        if(listeChambre.length == 0)
            listeChambre = Agenda.getInstance().AfficherChambreLibreParCategorie(sejour.getDateDebut(), sejour.getDateFin());
        
        
        celNames = new String[2];
        celNames[0] = "Numéro de la chambre";
        celNames[1] = "Catégorie";

        JTable jtbl = new JTable(listeChambre, celNames);
        this.tblChambreDispo.setModel(new DefaultTableModel());
        this.tblChambreDispo.setModel(jtbl.getModel());      
    }
    
    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        switch(evt.getPropertyName()){
            case SejourController.LISTE_CHAMBRE_PROPERTY:
                //populerTableSejour((String[][])evt.getNewValue());
                populerTableSejour(Agenda.getInstance().lireChambreSejour(sejour.getNum()));
                populerTableChambreLibre();
                break;
            case SejourController.CHANGE_CHAMBRE_PROPERTY:
                //this.getTopLevelAncestor().setVisible(false);
                break;
        }
    }
}
